
$(document).ready(function(){

    // 获取首页数据
    var indexoList = document.querySelector('#indexList');
    if( indexoList ){
        indexDataAjax(1);
	}
	
	// 详细页获取数据
	if( $("#newshow").length > 0 ){
        var newsId = localData.getData("newsId");
        getNewshowData(newsId); 
	}

})


// 获取首页数据
function indexDataAjax(pageNum){
    var indexoList = document.querySelector('#indexList');
    indexoList.pageNum = pageNum;
    $.ajax({
        url: 'http://192.168.1.80:7758/pub/article/query?cid=8c375c11-b76d-35ac-ad94-d9ae128e2a84&page.no=' + pageNum + '&page.size=10',
        type: "get",
        dataType: "json",
        success: function(data){
            if( data.code == "000000" ){ // 对接成功
                var item = {
                    iStart : 0,
                    _length : data.result.size,
                    isEnd : false
                };
                createIndexList( item, data.result.data );  // 创建首页list列表
                var scrollBar = document.querySelector('.scrollBar');
                if( !scrollBar ){
                    setIndexScroll( item, data.result.data ); // 设置首页滚动条
                }
            }else{
                alert("获取数据失败，请重试");
            } 
        },
        error: function () {
            alert("获取数据失败，请重试！");
        }
    });
}


// 创建首页list列表
function createIndexList(item, data){
    var indexoList = document.querySelector('#indexList');
    if( item._length > 0 ){
        for(var i=item.iStart; i<item._length; i++){
			var oSection = document.createElement("section");
            var time = getDate( data[i].publish_time_third ); // 毫秒数转为日期
            var imgSrc = data[i].imgs[0];
            if( !imgSrc ){
                imgSrc = "images/noimg.png";
            }
            oSection.innerHTML = `
					<a href="javascript:;" class="article_link clearfix">
						<div class="item-detail leftSlide">
							<h3 class="item_title">${data[i].title}</h3>
							<div class="item_info">
								<span>阅读量 ${data[i].view_count}</span>
								<span class="item-time">${time}</span>
							</div>
						</div>
						<div class="item-img">
							<img class="vPoster" src="${imgSrc}" />
						</div>
					</a>
            `;
            oSection.isLoad = true;
            indexoList.appendChild(oSection);
            oSection.index = i;
			newshowFn(oSection, oSection.index, data); //点击进入详细页面
        }

    }else{  // 没有更多数据了
        noMoreData(indexoList);
    }
}


// 设置首页滚动条
function setIndexScroll(item, data){
    var box = document.querySelector('.content');
    var inner = document.querySelector('.conScroll');
    var indexoList = document.querySelector('#indexList');
    var dropdownLoad = document.querySelector(".downLoading");
    mScroll({
        el:box,
        showBar: true, //为true表示显示滚动条
        dropUp: true, 
        dropDown: false, 
        isRefresh: true,
        start: function(e){
            var innerTop = css(inner,"translateY") - 5;
            var maxTop = box.clientHeight - inner.offsetHeight;
            if( innerTop <= maxTop ){ //到达最底部
                dropdownLoad.style.opacity = 1;
                item.isEnd = true;   
            } else {
                dropdownLoad.style.opacity = 0; 
                item.isEnd = false;  
            }
        },
        move: function(e){
            
        },
        end: function(e){
            var innerTop = css(inner,"translateY") - 5;
            var maxTop = box.clientHeight - inner.offsetHeight;
            var iNow = indexoList.pageNum;

            if( item.isEnd && innerTop <= maxTop ){ // 滚动到最底部了
                clearInterval(inner.timer);//清除定时阻止滑屏函数回弹动画
                document.querySelector('.scrollBar').style.opacity = 0;
                if( !indexoList.isLastPage ){
                    iNow++;
				}
                indexDataAjax(iNow);
                item.isEnd = false;
            }
        }
    })
}


// 没有更多数据了
function noMoreData(parentNode){
    var box = document.querySelector('.content');
    var inner = document.querySelector('.conScroll');
    var dropdownLoad = document.querySelector(".downLoading");
    var dropdownNoData = document.querySelector(".dropload-noData");
    var timer = "";

    dropdownLoad.style.opacity = 0;
    dropdownNoData.style.opacity = 1;
    parentNode.isLastPage = true;
    clearTimeout(timer);
    timer = setTimeout(function(){
        dropdownNoData.style.opacity = 0;
        MTween({
            el: inner,
            target:{ 
                translateY: box.clientHeight - inner.offsetHeight
            },
            time: 100,
            type: "easeBoth"
        });
    },1000);
    return;
}
 
// 毫秒数转为日期
function getDate( str ){
	var oDate = new Date(str);
	return toZero(oDate.getMonth() + 1) + "-" + toZero(oDate.getDate()) + " " + toZero(oDate.getHours()) + ":" + toZero(oDate.getMinutes());
}



// 日期单位数的补0
function toZero(obj){
	if(obj>=0 && obj <= 9){
		obj = '0' + obj;
	}
	return obj;
}



// 首页点击某个列表后记录对应的data，并进入到详细页面
function newshowFn(oSection, index, data){
    bind(oSection,"touchstart",function(ev){ 
        this.isMove = false;
    });
    bind(oSection,"touchmove",function(ev){ 
        this.isMove = true;
    });
    bind(oSection,"touchend",function(){ 
        if(this.isMove) return; // 如果是move事件就不执行点击事件 
        window.location.href = "newshow.html";
		localData.setData("newsId",data[index].id);
    });
}


// 获取详细页数据
function getNewshowData(newsId){
    $.ajax({
        url: 'http://192.168.1.80:7758/pub/article/get?cid=8c375c11-b76d-35ac-ad94-d9ae128e2a84&id=' + newsId,
        type: "get",
        dataType: "json",
        success: function(data){
            if( data.code == "000000" ){ // 对接成功
                var str = `
                	<h1 class="article_title">${data.result.title}</h1>
                	<div class="article_content">${data.result.content}</div>
                `;
                $("#newshow").append( str );
                setbPublicScroll();  // 设置通用滚动条
            }else{
                alert("获取数据失败，请重试");
            } 
        },
        error: function () {
            alert("获取数据失败，请重试！");
        }
    });
}
